package com.neiquan.backstage.resourec_manage.webaction;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.common.util.Strings;
import com.neiquan.backstage.platform_alliance_member.PlatformAllianceMemberService;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.PlatformAllianceMember;

@Controller
@RequestMapping(value = "allianceMember", produces = { "text/json;charset=UTF-8" })
public class AllianceMemberAction {
	private static Class<?> poc = PlatformAllianceMember.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private PlatformAllianceMemberService allianceMemberService;

	@ResponseBody
	@RequestMapping(value = "queryDetail")
	public String queryDetail(HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		return CommonResp.buildSuccessResp(allianceMemberService.queryById(loginedUser.getMemberId())).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "edit")
	public String edit(HttpSession session, String weixinSignKey,String weixinMchId,String website,String addr, String phone, String desc, String checkInDesc, String qrcodeUrl, String weixinAppId, String weixinAppSecret, String weixinToken, String weixinEncodingAesKey, String weixinH5UrlMembershipService, String weixinH5UrlConvenienceService, String weixinH5UrlEvent, String weixinH5UrlArticle, String weixinH5UrlEntrepreneurService, String weixinH5UrlEntrepreneur, String weixinH5UrlPartner) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		PlatformAllianceMember toUpdate = allianceMemberService.queryById(loginedUser.getMemberId());
		if (null == toUpdate) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		if (StringUtils.isNoneBlank(weixinSignKey)) {
			toUpdate.setWeixinSignKey(Strings.trimAndNotNull(weixinSignKey));
		}
		if (StringUtils.isNoneBlank(weixinMchId)) {
			toUpdate.setWeixinMchId(Strings.trimAndNotNull(weixinMchId));
		}
		if (StringUtils.isNoneBlank(addr)) {
			toUpdate.setAddr(Strings.trimAndNotNull(addr));
		}
		if (StringUtils.isNoneBlank(phone)) {
			toUpdate.setPhone(Strings.trimAndNotNull(phone));
		}
		if (StringUtils.isNoneBlank(desc)) {
			toUpdate.setDesc(Strings.trimAndNotNull(desc));
		}
		if (StringUtils.isNoneBlank(checkInDesc)) {
			toUpdate.setCheckInDesc(Strings.trimAndNotNull(checkInDesc));
		}
		if (StringUtils.isNoneBlank(website)) {
			toUpdate.setWebsite(Strings.trimAndNotNull(website));
		}
		if (StringUtils.isNoneBlank(qrcodeUrl)) {
			toUpdate.setQrcodeUrl(Strings.trimAndNotNull(qrcodeUrl));
		}
		if (StringUtils.isNoneBlank(weixinAppId)) {
			toUpdate.setWeixinAppId(Strings.trimAndNotNull(weixinAppId));
		}
		if (StringUtils.isNoneBlank(weixinAppSecret)) {
			toUpdate.setWeixinAppSecret(Strings.trimAndNotNull(weixinAppSecret));
		}
		if (StringUtils.isNoneBlank(weixinToken)) {
			toUpdate.setWeixinToken(Strings.trimAndNotNull(weixinToken));
		}
		if (StringUtils.isNoneBlank(weixinEncodingAesKey)) {
			toUpdate.setWeixinEncodingAesKey(Strings.trimAndNotNull(weixinEncodingAesKey));
		}
		if (StringUtils.isNoneBlank(weixinH5UrlMembershipService)) {
			toUpdate.setWeixinH5UrlMembershipService(Strings.trimAndNotNull(weixinH5UrlMembershipService));
		}
		if (StringUtils.isNoneBlank(weixinH5UrlConvenienceService)) {
			toUpdate.setWeixinH5UrlConvenienceService(Strings.trimAndNotNull(weixinH5UrlConvenienceService));
		}
		if (StringUtils.isNoneBlank(weixinH5UrlEvent)) {
			toUpdate.setWeixinH5UrlEvent(Strings.trimAndNotNull(weixinH5UrlEvent));
		}
		if (StringUtils.isNoneBlank(weixinH5UrlArticle)) {
			toUpdate.setWeixinH5UrlArticle(Strings.trimAndNotNull(weixinH5UrlArticle));
		}
		if (StringUtils.isNoneBlank(weixinH5UrlEntrepreneurService)) {
			toUpdate.setWeixinH5UrlEntrepreneurService(Strings.trimAndNotNull(weixinH5UrlEntrepreneurService));
		}
		if (StringUtils.isNoneBlank(weixinH5UrlEntrepreneur)) {
			toUpdate.setWeixinH5UrlEntrepreneur(Strings.trimAndNotNull(weixinH5UrlEntrepreneur));
		}
		if (StringUtils.isNoneBlank(weixinH5UrlPartner)) {
			toUpdate.setWeixinH5UrlPartner(Strings.trimAndNotNull(weixinH5UrlPartner));
		}
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	public static void main(String[] args) {
		List<String> list = Arrays.asList("addr", "phone", "desc", "checkInDesc", "qrcodeUrl", "weixinAppId", "weixinAppSecret", "weixinToken", "weixinEncodingAesKey", "weixinH5UrlMembershipService", "weixinH5UrlConvenienceService", "weixinH5UrlEvent", "weixinH5UrlArticle", "weixinH5UrlEntrepreneurService", "weixinH5UrlEntrepreneur", "weixinH5UrlPartner");
		for (String string : list) {
			System.out.println(String.format("if (StringUtils.isNoneBlank(%s)) {", string));
			System.out.println(String.format("toUpdate.set%s(Strings.trimAndNotNull(%s));", StringUtils.capitalize(string), string));
			System.out.println(String.format("}"));
		}
	}
}
